/**************************************************************************** 
 * Meta，一个轻量级的静态分区Hypervisor 
 *
 * 版权(c) , 2022-
 *
 * 作者:
 *      Tupelo Shen <shenwanjiang2013@163.com>
 *
 * Meta是一个自由软件，你可以在遵循GNU GPLv2协议的条件下，重新修改并发布它。
 *
 ***************************************************************************/

#ifndef __INTERRUPTS_H__
#define __INTERRUPTS_H__

#include <meta.h>
#include <arch/interrupts.h>

#include <bitmap.h>

struct vm;

typedef void (*irq_handler_t)(irqid_t int_id);

/****************************************************************************
 ************************** 公共函数接口 ************************************
 ****************************************************************************/
void interrupts_reserve(irqid_t int_id, irq_handler_t handler);
void interrupts_init(void);
void interrupts_arch_init(void);

bool interrupts_check(irqid_t int_id);
void interrupts_clear(irqid_t int_id);

bool interrupts_arch_check(irqid_t int_id);
void interrupts_arch_clear(irqid_t int_id);

/* 使能某个中断 */
void interrupts_cpu_enable(irqid_t int_id, bool en);
void interrupts_arch_enable(irqid_t int_id, bool en);

/* 发送核间中断 */
void interrupts_cpu_sendipi(cpuid_t target_cpu, irqid_t ipi_id);
void interrupts_arch_ipi_send(cpuid_t cpu_target, irqid_t ipi_id);

/* 中断注入VM */
void interrupts_vm_inject(struct vm *vm, irqid_t id);
void interrupts_arch_vm_inject(struct vm *vm, irqid_t id);

/* 中断绑定到VM */
void interrupts_vm_assign(struct vm *vm, irqid_t id);
void interrupts_arch_vm_assign(struct vm *vm, irqid_t id);
bool interrupts_arch_conflict(bitmap_t* interrupt_bitmap, irqid_t id);

enum irq_res { HANDLED_BY_HYP, FORWARD_TO_VM };
enum irq_res interrupts_handle(irqid_t int_id);

#endif /* __INTERRUPTS_H__ */
